package com.fc.backingbeans.recipe;

import javax.annotation.PostConstruct
import javax.faces.bean.RequestScoped

import org.primefaces.event.FileUploadEvent
import org.primefaces.model.UploadedFile
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

import com.fc.backingbeans.AbstractDialog
import com.fc.entities.Food
import com.fc.entities.Recipe
import com.fc.entities.RecipeEntry
import com.fc.service.UserDetailsService
import com.fc.service.FoodService
import com.fc.service.RecipeService
import com.fc.utils.MessageUtils

/**
 * 
 * @author Ács Ádám
 *
 */
@Component("addRecipeDialog")
@RequestScoped
public class AddRecipeDialog extends AbstractDialog {
	@Autowired private RecipeService recipeService
	@Autowired private FoodService foodService
	@Autowired private UserDetailsService userDetailsService
	Recipe recipe
	RecipeEntry recipeEntry

	@Override
	void onShow() {
		recipeEntry = new RecipeEntry()
		recipe = new Recipe()
		title = "Recept hozzáadása"
	}

	void saveRecipe() {
		recipe.user = userDetailsService.currentUser
		recipeService.create(recipe)
	}

	void addEntry() {
		recipe.ingredients.add(recipeEntry)
		recipeEntry = new RecipeEntry()
	}

	void removeEntry(RecipeEntry recipeEntry) {
		recipe.ingredients.remove(recipeEntry)
	}

	List<Food> findFoods() {
		def foods = foodService.findAllDefault()
		foods += foodService.findAllByUser(userDetailsService.currentUser)
		return foods
	}

	@Override
	String getDialogName() {
		'fc/food/addRecipe'
	}

	@Override
	Map getOptions() {
		[
			modal : true
			, draggable:false
			, resizable: false
			,contentHeight : 500
			, contentWidth : 800,
			,width: 850
			,height: 550

		]
	}
}
